'use strict';

goog.provide('Blockly.JavaScript.servo');

goog.require('Blockly.JavaScript');

Blockly.JavaScript.servo_move = function() {
  var dropdown_pin = Blockly.JavaScript.valueToCode(this, 'PIN', Blockly.JavaScript.ORDER_ATOMIC);
  var value_degree = Blockly.JavaScript.valueToCode(this, 'DEGREE', Blockly.JavaScript.ORDER_ATOMIC);

  var new_pin=null;
  switch (dropdown_pin){
    case "A0":
      new_pin='14';
      break;
    case "A1":
      new_pin="15";
      break;
    case "A2":
      new_pin="16";
      break;
    case "A3":
      new_pin="17";
      break;
    case "A4":
      new_pin="18";
      break;
    case "A5":
      new_pin="19";
      break;
    default:
      new_pin=dropdown_pin;
      break;
  }

  var fun_name='servo_move_'+Math.round(Math.pow(Math.random(),3)*100000000);

  var set_pin_mode='ActionBlock.exe_bock_not_reply({"method": "servo_config", "params": ["'+new_pin+'", "544","2400"]});\n';
  var pin_write='function '+fun_name+ '() {\nActionBlock.exe_bock_has_reply({"method": "analog_write", "params": ["'+new_pin+'", '+value_degree+']},0);\n' +
    'ActionBlock.set_panel_write_data("'+dropdown_pin+'",'+value_degree+',0);\n}';
  Blockly.JavaScript.definitions_['set_pin_mode_'+new_pin]=set_pin_mode;
  Blockly.JavaScript.definitions_[fun_name]=pin_write;

  var code = fun_name+'();\n';
  return code;
};


Blockly.JavaScript.servo_read_degrees = function() {
  var dropdown_pin = Blockly.JavaScript.valueToCode(this, 'PIN', Blockly.JavaScript.ORDER_ATOMIC);
  var code = '';
  return code;
};


Blockly.JavaScript.green_servo_move = function() {
  var dropdown_pin = '7';
  var value_degree = Blockly.JavaScript.valueToCode(this, 'DEGREE', Blockly.JavaScript.ORDER_ATOMIC);

  var new_pin=dropdown_pin;

  var fun_name='servo_move_'+Math.round(Math.pow(Math.random(),3)*100000000);

  var set_pin_mode='ActionBlock.exe_bock_not_reply({"method": "servo_config", "params": ["'+new_pin+'", "544","2400"]});\n';
  var pin_write='function '+fun_name+ '() {\nActionBlock.exe_bock_has_reply({"method": "analog_write", "params": ["'+new_pin+'", '+value_degree+']},0);\n' +
    'ActionBlock.set_panel_write_data("'+dropdown_pin+'",'+value_degree+',0);\n}';
  Blockly.JavaScript.definitions_['set_pin_mode_'+new_pin]=set_pin_mode;
  Blockly.JavaScript.definitions_[fun_name]=pin_write;

  var code = fun_name+'();\n';
  return code;
};

Blockly.JavaScript.white_servo_move = function() {
  var dropdown_pin = '11';
  var value_degree = Blockly.JavaScript.valueToCode(this, 'DEGREE', Blockly.JavaScript.ORDER_ATOMIC);

  var new_pin=dropdown_pin;

  var fun_name='servo_move_'+Math.round(Math.pow(Math.random(),3)*100000000);

  var set_pin_mode='ActionBlock.exe_bock_not_reply({"method": "servo_config", "params": ["'+new_pin+'", "544","2400"]});\n';
  var pin_write='function '+fun_name+ '() {\nActionBlock.exe_bock_has_reply({"method": "analog_write", "params": ["'+new_pin+'", '+value_degree+']},0);\n' +
    'ActionBlock.set_panel_write_data("'+dropdown_pin+'",'+value_degree+',0);\n}';
  Blockly.JavaScript.definitions_['set_pin_mode_'+new_pin]=set_pin_mode;
  Blockly.JavaScript.definitions_[fun_name]=pin_write;

  var code = fun_name+'();\n';
  return code;
};

Blockly.JavaScript.blue_servo_move = function() {
  var dropdown_pin = '8';
  var value_degree = Blockly.JavaScript.valueToCode(this, 'DEGREE', Blockly.JavaScript.ORDER_ATOMIC);

  var new_pin=dropdown_pin;

  var fun_name='servo_move_'+Math.round(Math.pow(Math.random(),3)*100000000);

  var set_pin_mode='ActionBlock.exe_bock_not_reply({"method": "servo_config", "params": ["'+new_pin+'", "544","2400"]});\n';
  var pin_write='function '+fun_name+ '() {\nActionBlock.exe_bock_has_reply({"method": "analog_write", "params": ["'+new_pin+'", '+value_degree+']},0);\n' +
    'ActionBlock.set_panel_write_data("'+dropdown_pin+'",'+value_degree+',0);\n}';
  Blockly.JavaScript.definitions_['set_pin_mode_'+new_pin]=set_pin_mode;
  Blockly.JavaScript.definitions_[fun_name]=pin_write;

  var code = fun_name+'();\n';
  return code;
};

Blockly.JavaScript.black_servo_move = function() {
  var dropdown_pin = '13';
  var value_degree = Blockly.JavaScript.valueToCode(this, 'DEGREE', Blockly.JavaScript.ORDER_ATOMIC);

  var new_pin=dropdown_pin;

  var fun_name='servo_move_'+Math.round(Math.pow(Math.random(),3)*100000000);

  var set_pin_mode='ActionBlock.exe_bock_not_reply({"method": "servo_config", "params": ["'+new_pin+'", "544","2400"]});\n';
  var pin_write='function '+fun_name+ '() {\nActionBlock.exe_bock_has_reply({"method": "analog_write", "params": ["'+new_pin+'", '+value_degree+']},0);\n' +
    'ActionBlock.set_panel_write_data("'+dropdown_pin+'",'+value_degree+',0);\n}';
  Blockly.JavaScript.definitions_['set_pin_mode_'+new_pin]=set_pin_mode;
  Blockly.JavaScript.definitions_[fun_name]=pin_write;

  var code = fun_name+'();\n';
  return code;
};

Blockly.JavaScript.red_servo_move = function() {
  var dropdown_pin = '4';
  var value_degree = Blockly.JavaScript.valueToCode(this, 'DEGREE', Blockly.JavaScript.ORDER_ATOMIC);

  var new_pin=dropdown_pin;

  var fun_name='servo_move_'+Math.round(Math.pow(Math.random(),3)*100000000);

  var set_pin_mode='ActionBlock.exe_bock_not_reply({"method": "servo_config", "params": ["'+new_pin+'", "544","2400"]});\n';
  var pin_write='function '+fun_name+ '() {\nActionBlock.exe_bock_has_reply({"method": "analog_write", "params": ["'+new_pin+'", '+value_degree+']},0);\n' +
    'ActionBlock.set_panel_write_data("'+dropdown_pin+'",'+value_degree+',0);\n}';
  Blockly.JavaScript.definitions_['set_pin_mode_'+new_pin]=set_pin_mode;
  Blockly.JavaScript.definitions_[fun_name]=pin_write;

  var code = fun_name+'();\n';
  return code;
};
